<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading v-if="loading" class="article-loading" />
    <!-- /加载中 loading -->
     <!-- 文章找不到的页面 -->
    <div class="error" v-if="is404">
      <p>文章被外星人吃掉了</p>
      <van-button @click="$router.back()">后退</van-button>
      <van-button @click="$router.push('/')">回主页</van-button>
    </div>
    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{article.title}}</h3>
      <div class="author">
        <!-- <van-image round width="1rem" height="1rem" fit="fill" />
        <div class="text">
          <p class="name">{{article.aut_name}}</p>
          <p class="time">{{article.pubdate | relativeTime}}</p>
        </div> -->
       <author :author ='article'>
        <van-button
          flex
          round
          size="small"
          type="info"
          @click="hSwitchFollow"
        >{{article.is_followed? '取关': '关注'}}</van-button>
        </author>
      </div>
      <div class="content">
        <div v-html="article.content"></div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button round size="small"
        hairline type="primary"
        plain
        :icon="article.attitude === 1 ? 'good-job' : 'good-job-o'"
        @click="hSwitchLike">{{article.attitude === 1? '取消点赞' : '点赞'}}</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
        round
        size="small"
        hairline type="danger"
        plain
        icon="delete"
        @click="hSwitchDisLike">{{article.attitude === 0? '取消踩' : '踩'}}</van-button>
      </div>
      <!-- 文章评论 -->
      <comment :acticleID = "acticleID"></comment>
      <!-- 文章评论 -->
    </div>

  </div>
</template>

<script>
// 调用接口获取文章详情
// 引入点赞取消点赞接口
import { getArticle, deleltLike, addLike, deleltDisLike, addDisLike } from '@/api/article.js'
// 引入关注 雨取消关注接口
import { followUser, unFollowUser } from '@/api/user.js'
// 引入作者组件
import author from '@/components/author'
// 引入评论组件
import Comment from './comment'
export default {
  name: 'ArticleIndex',
  components: {
    author,
    Comment
  },
  data () {
    return {
      is404: false, // 文章是否存在
      loading: false, // 控制加载中的 loading 状态
      article: { }, // 文章详情数据
      acticleID: null // 文章id
    }
  },
  created () {
    this.acticleID = this.$route.params.id
    this.hGetArticle()
  },
  // 路由守卫
  beforeRouteLeave (to, from, next) {
    if (to.path === '/login') {
      if (this.$store.state.tokenInfo.token) {
        this.$router.push('/')
      } else {
        next()
      }
    } else {
      (
        next()
      )
    }
  },
  methods: {
    // 发送请求获取文章详情
    async hGetArticle () {
      // 设置loading
      this.loading = true
      // 发送请求
      try {
        const res = await getArticle(this.acticleID)
        // console.log('文章详情', res)
        this.article = res.data.data
        // 设置loading
        this.loading = false
      } catch (err) {
        this.is404 = (err.response && err.response.status === 404)
        this.$toast.fail('加载失败')
        this.loading = false
      }
    },
    // 取消关注 加关注
    async hSwitchFollow () {
      // 解构赋值
      const { is_followed: isFollowed, aut_id: autId } = this.article
      if (isFollowed) {
        // 已经关注 取消关注
        await unFollowUser(autId.toString())
        // 修改视图
        this.article.is_followed = false
        this.$toast.success('取关成功')
      } else {
        // 没有关注 进行关注
        await followUser(autId.toString())
        // 修改视图
        this.article.is_followed = true
        this.$toast.success('关注成功')
      }
    },
    // 点赞与取消点赞
    async hSwitchLike () {
      // 1 代表点赞 -1 代表无状态
      // 根据当前attitude 是哪个决定调用哪一个方法
      // attitude 是1 那么就是点赞 要调用取消点赞接口
      if (this.article.attitude === 1) {
        await deleltLike(this.article.art_id.toString())
      } else {
        await addLike(this.article.art_id.toString())
      }
      this.$toast.success('操作成功')
      //   更新视图
      this.article.attitude = this.article.attitude * -1
    },
    async hSwitchDisLike () {
      // 当attitude 为0时 代表不喜欢
      if (this.article.attitude === 0) {
        //    取消不喜欢
        await deleltDisLike(this.article.art_id.toString())
        this.article.attitude = -1
      } else {
        // 添加不喜欢
        await addDisLike(this.article.art_id.toString())
        this.article.attitude = 0
      }
      this.$toast.success('操作成功')
    }
  }
}
</script>

<style scoped lang='less'>
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
